package com.xique.door.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.xique.common.core.text.Convert;
import com.xique.common.core.utils.DateUtils;
import com.xique.common.datascope.annotation.NeedSetFieldValue;
import com.xique.door.bean.domain.CardDevice;
import com.xique.door.bean.domain.DCard;
import com.xique.door.command.ICardCommandBizService;
import com.xique.door.mapper.DCardMapper;
import com.xique.door.service.*;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 卡片Service业务层处理
 *
 * @author ww
 * @date 2021-09-14
 */
@Service
public class DCardServiceImpl implements IDCardService {

    @Autowired
    private DCardMapper dCardMapper;

    @Autowired
    private IDCardDeviceService cardDeviceService;

    @Autowired
    private ICardCommandBizService cardCommandBizService;

    @Autowired
    private IAccessDeviceService accessDeviceService;

    @Autowired
    private IUserService userService;

    @Autowired
    private IDCardHouseService cardHouseService;

    @Autowired
    private IDCardService dCardService;

    @Autowired
    private IBuildHouseService buildHouseService;


    /**
     * 查询卡片
     *
     * @param id 卡片ID
     * @return 卡片
     */
    @Override
    @NeedSetFieldValue
    public DCard selectDCardById(Long id) {
        return dCardMapper.selectDCardById(id);
    }

    /**
     * 查询门禁卡片信息
     *
     * @param cardIds 门禁卡片信息ID
     * @return 门禁卡片信息
     */
    @Override
    @NeedSetFieldValue
    public List<DCard> selectCardByIds(List<Long> cardIds) {
        return dCardMapper.selectDCardByIds(cardIds, null);
    }

    /**
     * 查询卡片列表
     *
     * @param dCard 卡片
     * @return 卡片
     */
    @Override
    @NeedSetFieldValue
    public List<DCard> selectDCardList(DCard dCard) {
        return dCardMapper.selectDCardList(dCard);
    }

    /**
     * 新增卡片
     *
     * @param dCard 卡片
     * @return 结果
     */
    @Override
    public int insertDCard(DCard dCard) {
        dCard.setCreateTime(DateUtils.getNowDate());

        return dCardMapper.insertDCard(dCard);
    }

    /**
     * 修改卡片
     *
     * @param dCard 卡片
     * @return 结果
     */
    @Override
    public int updateDCard(DCard dCard) {
        dCard.setUpdateTime(DateUtils.getNowDate());
        return dCardMapper.updateDCard(dCard);
    }

    /**
     * 解绑卡片与用户房屋的关系
     *
     * @param cardId 卡片id
     * @return 结果
     */
    public int updateUserHouse(Long cardId) {
        return dCardMapper.updateUserHouse(cardId);
    }


    /**
     * 删除卡片对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteDCardByIds(String ids) {
        return dCardMapper.deleteDCardByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除卡片信息
     *
     * @param id 卡片ID
     * @return 结果
     */
    @Override
    public int deleteDCardById(Long id) {
        return dCardMapper.deleteDCardById(id);
    }

    @Override
    public int deleteDCardByUserId(Long userId) {
        return dCardMapper.deleteDCardByUserId(userId);
    }

    @Override
    public List<DCard> selectDCardByIds(List<Long> cardIds, Integer status) {
        return dCardMapper.selectDCardByIds(cardIds, status);
    }

    @Override
    public List<DCard> selectDCardByUserIds(List<Long> userIds, Integer status) {
        if (CollectionUtils.isEmpty(userIds)) {
            return new ArrayList<>();
        }
        return dCardMapper.selectDCardByUserIds(userIds, status);
    }

    @Override
    public List<DCard> selectDCardByCardSns(List<String> cardSns, Integer status) {
        return dCardMapper.selectDCardByCardSns(cardSns, status);
    }

    @Override
    public List<DCard> selectCardList(DCard dCard) {
        return dCardMapper.selectCardList(dCard);
    }

    @Override
    public List<DCard> selectConditionCardList(DCard dCard) {

        return dCardMapper.selectConditionCardList(dCard);
    }

    @Override
    public int deleteDCardByIdList(List<Long> ids) {
        if (CollectionUtil.isEmpty(ids)) {
            return 1;
        }
        return dCardMapper.deleteDCardByIdList(ids);
    }

    @Override
    public List<CardDevice> selectDCardDeviceList(Long id) {
        return dCardMapper.selectDCardDeviceList(id);
    }

    @Override
    public int deleteDCardDeviceId(Long id) {
        return dCardMapper.deleteDCardDeviceId(id);
    }

}
